<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv=X-UA-Compatible content="IE=edge,chrome=1">
    <meta content=always name=referrer>
    <link rel="stylesheet" type="text/css" href="__STATIC__/css/renzheng.css">
    <script src="__STATIC__/js/jquery-1.8.2.js"></script>
    <title>更换绑定手机号</title>
</head>
<body>
<form action="" method="post" id="form" onsubmit="return false">
<div class="wrap-pass">
    <div class="header">
        <p>更换绑定手机号</p>
        <img src="__STATIC__/img/back.png" class="back" onclick =window.location.href="{:url('mobile/shop_set/safety')}";>
    </div>
    <h3>验证身份</h3>
    <div class="center">
        <div>
            <input class="text" type="text" value="{$username}" name="username" placeholder="手机号码" readonly>
        </div>
        <div>
            <input class="text" type="text" placeholder="验证码" name="yzm_1" datatype="n6" nullmsg="请填写验证码" errormsg="请填写6位纯数字">
            <span><a href="javascript:check_num(1);" id="yzm_1" >获取验证码</a></span>
        </div>
    </div>
    <h3>设置新的绑定手机</h3>
    <div class="center">
        <div>
            <input class="text" type="text" name="new_phone" placeholder="手机号码" datatype="m" errormsg="号码格式不正确" nullmsg="请填写新的绑定手机号码">
        </div>
        <div>
            <input class="text" type="text" placeholder="验证码" name="yzm_2" datatype="n6" nullmsg="请填写验证码" errormsg="请填写6位纯数字">
            <span><a href="javascript:check_num(2);" id="yzm_2" >获取验证码</a></span>
        </div>
    </div>
    <input type="button" class="submit" value="确定">
</div>
</form>
{include file="public/src"  /}
<script>
    $(function () {
        $.Tipmsg.r = null;
        $.Tipmsg.p = null;
        $.Datatype.n6 = /^\d{6}$/;
        var demo = $("#form").Validform({
            tiptype: function (msg) {
                layer.open({content: msg, skin: 'msg', time:2});
            },
            btnSubmit:'.submit',
            tipSweep: true,
            ajaxPost:true,
            callback:function(t){
                console.log(t);
                layer.open({content:t.message,skin:'msg',time:2});
                setTimeout(function () {
                    window.location.href = "safety";
                },2000);
            }
        });
        demo.config({
            url:"{:url('mobile/shop_set/bind_act')}"
        })
    });
    //发送验证码
    function check_num(id){
        var tel = '';
        if (id == 1){
            tel = $("[name='username']").val();
        }else{
            tel = $("[name='new_phone']").val();
            if (tel == ''){
                layer.open({content: '请输入新的绑定手机号码', skin: 'msg', time:2});
                return;
            }
            if (!(/^1(3|4|5|7|8)\d{9}$/.test(tel))){
                layer.open({content: '新的绑定手机号码格式不正确', skin: 'msg', time:2});
                return;
            }
            if (!check_unique(tel)){
                return;
            }
        }
        count_down(id);
        $.ajax({
            url:"{:url('mobile/shop_set/get_code')}",
            dataType:'json',
            type:'post',
            data:{'tel':tel,'act':id},
            success:function(t){
                if (t.code != 200){
                    layer.open({content: t.message,skin: 'msg',time:2});
                }
            }
        })
    }
    //验证码倒计时
    function count_down(id){
        var num = 60;
        var verif = $('#yzm_'+id);
        var timer = setInterval(function(){
            verif.text(num+'秒');
            if (num <=60 && num >= 1){
                verif.removeAttr('href');
            }else{
                verif.text('重新获取验证码');
                window.clearInterval(timer);
                verif.attr("href",'javascript:check_num(this.id);');
            }
            num--;
        },1000);
    }
    //检测新绑定的手机号码的唯一性
    function check_unique(tel){
        var flag = true;
        $.ajax({
            url:"{:url('mobile/shop_set/bind_phone')}",
            type:'post',
            async:false,
            dateType:'json',
            data:{'tel':tel},
            success:function(t){
                if (t.code != 200){
                    layer.open({content: t.message,skin: 'msg',time:2});
                    flag = false;
                }
            }
        });
        return flag;
    }
</script>
</body>
</html>